package com.yang;

import androidx.appcompat.app.AppCompatActivity;

import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.widget.Button;
import android.widget.SeekBar;

import java.util.Timer;
import java.util.TimerTask;

public class MainActivity extends AppCompatActivity {

    private SeekBar seekBar;

    private Button startBtn, continueBtn, pauseBtn, stopBtn;

    private MusicService.MyBinder myBinder;

    private MusicServiceConnection musicServiceConnection;

    private Timer timer;

    private TimerTask timerTask;

    private boolean isChanging = false;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        seekBar = findViewById(R.id.seekBar);
        startBtn = findViewById(R.id.startBtn);
        continueBtn = findViewById(R.id.continueBtn);
        pauseBtn = findViewById(R.id.pauseBtn);
        stopBtn = findViewById(R.id.stopBtn);
        Intent intent = new Intent(MainActivity.this, MusicService.class);
        musicServiceConnection = new MusicServiceConnection();
        this.bindService(intent, musicServiceConnection, BIND_AUTO_CREATE);
        seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int i, boolean b) {
                if (myBinder != null && isChanging) {
                    myBinder.setProgressPosition(i);
                }
            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {
                isChanging = true;
            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {
                isChanging = false;
            }
        });
        startBtn.setOnClickListener(view -> {
            timer = new Timer();
            if (myBinder != null) {
                myBinder.startPlay();
            }
            timerTask = new TimerTask() {
                @Override
                public void run() {
                    if (isChanging) {
                        return;
                    } else {
                        if (myBinder != null) {
                            seekBar.setProgress(myBinder.getProgress());
                        }
                    }
                }
            };
            timer.schedule(timerTask, 0, 10);
        });
        pauseBtn.setOnClickListener(view -> {
            if (myBinder != null) {
                myBinder.pausePlay();
            }
        });
        continueBtn.setOnClickListener(view -> {
            if (myBinder != null) {
                myBinder.continuePlay();
            }
        });
        stopBtn.setOnClickListener(view -> {
            if (myBinder != null) {
                myBinder.stopPlay();
                unbindService(musicServiceConnection);
                myBinder = null;
            }
        });
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (myBinder != null) {
            myBinder = null;
        }
    }

    class MusicServiceConnection implements ServiceConnection {

        @Override
        public void onServiceConnected(ComponentName componentName, IBinder iBinder) {
            myBinder = (MusicService.MyBinder) iBinder;
        }

        @Override
        public void onServiceDisconnected(ComponentName componentName) {

        }
    }
}